Skip to content

Conversation

@kjanat
Copy link

@kjanat kjanat commented Jan 4, 2026

Summary

  • Add prog style option to customize program name color in usage text
  • Fix plugin to work with any argparse parser (not just those using RichHelpFormatter)
  • Add tests for plain parser support and style options
  • Update README to clarify compatibility and document style options

Problem

The plugin previously assumed the user's parser already used RichHelpFormatter. Most parsers use the default argparse.HelpFormatter, resulting in plain text with no colors.

Solution

Temporarily set RichHelpFormatter on the parser before calling format_help(), then restore the original formatter. This ensures colored output for any parser without requiring user modifications.

Changes

  1. _capture_help(): Temporarily swap formatter_class to RichHelpFormatter
  2. RichArgparseStyles: Add prog option (valid argparse.prog style in rich-argparse)
  3. Tests: Add coverage for plain parsers and style options
  4. README: Clarify any-parser support, document all style options
  5. Type fixes: Add cast() for choices dict values (fixes ty/mypy errors)

Copilot AI review requested due to automatic review settings January 4, 2026 08:12
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enhances the mkdocs-rich-argparse plugin to work with any argparse parser by temporarily swapping formatter classes, adds a prog style configuration option, and improves documentation about parser compatibility.

  • Temporarily applies RichHelpFormatter to any parser during help text capture to enable colored output
  • Adds prog style option to customize program name coloring
  • Updates README to clarify that the plugin works with any ArgumentParser, not just those using RichHelpFormatter
  • Adds type casting for subparser choices dictionary values to resolve type checking issues

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/mkdocs_rich_argparse/init.py Implements formatter class swapping in _capture_help() with proper restoration, adds prog style config option, and adds type casting for subparser choices
tests/test_plugin.py Adds tests verifying plain parsers get colors, formatter restoration for main and subparsers, prog style option application, and validates all style options exist
README.md Updates description to emphasize compatibility with any argparse parser and documents all style configuration options with examples

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +54 to +62
styles:
prog: "#00ff00" # Program name in usage
args: "#ff0000" # Arguments and options
groups: "#00ffff" # Group headers
metavar: "#ff00ff" # Metavariables (e.g., FILE)
help: white # Help text
text: white # Description text
default: grey50 # Default values
```
Copy link

Copilot AI Jan 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "syntax" style option is defined in the RichArgparseStyles config class but is missing from the README documentation. This creates an inconsistency between the available configuration options and the documentation.

Consider adding "syntax" to the list of style options in the README's Custom Styles section, or if it's intentionally excluded, add a comment explaining why.

Copilot uses AI. Check for mistakes.
Copy link
Member

@sverhoeven sverhoeven left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution and allowing this package to be used with just argparse.

I am able to see the green prog text

Image

Also a parser with default formatter works.

However when someone has configured a class_formatter then it gets replaced by the RichHelpFormatter and causing loss of the users intent.

For example when using ArgumentDefaultsHelpFormatter the (default 42) text is gone with this PR.

At https://github.com/hamdanal/rich-argparse/blob/129fd67f7d92cffb3402422112fde6b3121352f6/rich_argparse/_argparse.py#L528-L549 there are replacements for the argparse builtin formatters.

Can you make swapping the formatter_class for RichHelpFormatter smarter so at least the argparse builtin formatters are still functional?

Also can you also add the prog style to example/mkdocs-custom.yml ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants